<template>
  <main class="box_center">
    <!-- form表单项 -->
    <el-form class="login_form" :rules="rules" ref="login-form" :model="user">
      <div class="logo_imgs">
        <img src="./login_imgs/logo.png" alt="" />
      </div>
      <p class="spn">大运河文旅企业服务平台</p>
      <!-- 用户名输入框 -->
      <el-form-item prop="name">
        <el-input
          prefix-icon="el-icon-user"
          v-model="user.name"
          placeholder="请输入用户名称"
          autofocus
        ></el-input>
      </el-form-item>
      <!-- 用户密码输入框 -->
      <el-form-item prop="code">
        <el-input
          prefix-icon="el-icon-lock"
          type="password"
          v-model="user.code"
          placeholder="请输入登录密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="login_btn"
          :class="true_btn"
          type="info"
          @click="onLogin"
          :loading="login_loading"
          :disabled="btnState"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </main>
</template>

<script>
import { logins } from "@/api/connector/connector.js";
export default {
  name: "login",
  components: {},
  props: {},
  data() {
    return {
      user: {
        name: "",
        code: "",
      },
      login_loading: false,
      btnState: true,
      true_btn: "",
      login_loading: false,
      // 用户名和密码验证
      rules: {
        name: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: "用户名错误",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "用户密码不能为空", trigger: "change" },
          { pattern: /^(0|[1-9]|[1-4][0-9]|50)$/, message: "用户密码错误", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    valueName() {
      const { name, code } = this.user;
      return { name, code };
    },
  },
  watch: {
    valueName(value) {
      if (value.name == "" && value.code == "") {
        this.btnState = true;
        this.true_btn = "";
      } else {
        this.btnState = false;
        this.true_btn = "btn";
      }
    },
    "user.name"(value) {
      if (value !== "") {
      }
    },
  },
  created() {},
  mounted() {},
  methods: {
    onLogin() {
      // from表单验证
      this.$refs["login-form"].validate((valid) => {
        if (!valid) {
          return;
        }

        // 验证通过，请求登录
        this.logins();
      });
    },
    // 验证成功调用
     logins() {
      // 表单验证时的loding
      this.login_loading = true;
      // 验证通过，提交登录
      logins(this.user)
        .then((res) => {
          console.log(res);
          // 登录成功返回结果
          this.$message({
            message: "登录成功",
            type: "success",
          });
          // 跳转到主页
          this.$router.push("/");
          // 登录成功关闭loading
          this.login_loading = false;
        })
        .catch(() => {
          // 登录失败返回错误信息
          this.$message({
            message: "账号或密码错误",
            type: "warning",
          });
          // 登录失败也关闭loading
          this.login_loading = false;
        });
      // 处理后端响应结果
      // 成功：xxx
      // 失败：xxx
    },
  },
};
</script>

<style scoped lang="scss">
.box_center {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url(./login_imgs/banner.png) no-repeat center;
  background-size: 100% 100%;
  &::before {
    content: "";
    display: block;
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 1;
    background: #004fe0;
    opacity: 0.5;
  }
  .login_form {
    width: 405px;
    height: 414px;
    border-radius: 8px;
    background-color: rgba(255, 255, 255, 1);
    box-shadow: 0px 0px 10px -3px;
    padding: 48px 56px;
    z-index: 2;
    .logo_imgs {
      width: 163px;
      height: 58px;
      margin: auto;
      display: flex;
      justify-content: space-between;
      margin-bottom: 16px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .spn {
      height: 28px;
      font-size: 20px;
      font-weight: 600;
      color: #000000;
      line-height: 28px;
      margin-bottom: 48px;
    }
  }
  .login_btn {
    margin-top: 30px;
    width: 100%;
    height: 50px;
    font-size: 20px;
    font-weight: 600px;
    letter-spacing: 10px;
  }
  /deep/.el-input__inner {
    font-size: 16px;
    padding-left: 40px;
  }
  /deep/.el-icon-user,
  /deep/.el-icon-lock {
    font-size: 24px;
    display: block;
  }
}
.btn {
  background-color: #004fe0;
}
.false_btn {
  background-color: #b8c0cc;
}
/deep/.img_color {
  color: black;
}
</style>